/* confirm_command.c
 *
 * Copyright 2007-2008 OpenMoko, Inc.
 * Authored by Tick <tick@openmoko.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <pk-package-id.h>
#include <confirm_command.h>
#include <string.h>
#include <stdlib.h>
#include <debug.h>
#include <assassin.h>
#include <gears.h>
#include <actions.h>

extern Ecore_Evas * ee;

static int __delay_action(void *data) {
    struct Target *target = (struct Target *)((struct Installer_Alert *) data)->data;
    free_Alert((struct Installer_Alert*)data);
    action(target);
    return 0;
}

static int __get_answered__ = 0;
static void assassin_confirm_positive (void *data,  Evas_Object *obj, const char *emission, const char *source) {
    struct Target *target = (struct Target *)((struct Installer_Alert *) data)->data;
    struct Installer_Alert * AA = (struct Installer_Alert *) data;
    int x, y, w, h;
    debug("Yes selected!!\n");
    if (__get_answered__) return;
    __get_answered__ = 1;

    debug("target->package=%s\n", target->package);
    ecore_timer_add(2*FINISH_TIME_MAGIC, &__delay_action, data);
    evas_object_geometry_get(AA->alert, &x, &y, &w, &h);
    animator_move_object(AA->alert, FINISH_TIME_MAGIC, x, y, x, 0 - h);

}
static int __delay_free_Alert(void *data) {
    struct Target *target = (struct Target *)((struct Installer_Alert *) data)->data;
    struct Installer_Alert * AA = (struct Installer_Alert *) data;
    target_free(target);
    free_Alert(AA);
    return 0;
}
static void assassin_confirm_negative (void *data,  Evas_Object *obj, const char *emission, const char *source) {
    struct Installer_Alert * AA = (struct Installer_Alert *) data;
    int x, y, w, h;
    debug("No selected!!\n");
    if (__get_answered__) return;
    __get_answered__ = 1;

    remove_the_mask_on_screen();
    evas_object_geometry_get(AA->alert, &x, &y, &w, &h);
    animator_move_object(AA->alert, FINISH_TIME_MAGIC, x, y, x, 0 - h);
    ecore_timer_add(3*FINISH_TIME_MAGIC, &__delay_free_Alert, AA);
}


struct Installer_Alert *  do_confirm(Evas *evas, struct Target * target) {
    char name[MAX_PACKAGE_NAME_SIZE];
    char *type;
    struct Installer_Alert *AA;
    CallbackFunc pos, neg;
    neg = &assassin_confirm_negative;
    pos = &assassin_confirm_positive;
    if (target->alias) {
        strncpy(name, target->alias, sizeof(name));
    } else if (!get_pk_name_from_id(target->package, NULL, name, sizeof(name))) {
        error("Cannot get package name from ID\n");
        return NULL;
    }
    if (target->type == INSTALL) {
        type = "INSTALL";
    } else if (target->type == UPDATE) {
        type = "UPDATE";
    } else if (target->type == UNINSTALL) {
        type = "UNINSTALL";
    } else {
        error("Invalid target type\n");
        return NULL;
    }
    debug("target->package=%s \n", target->package);
    __get_answered__ = 0;
    put_a_mask_on_screen();
    AA = new_Alert(evas, type, name, "Yes", "No", target);
    if (!AA) {
        error("Create new Alert Failed!!!!\n");
        exit(1);
    }
    alert_set_callback(AA, pos, neg);
    target->status = WAITCONFIRM;
    return AA;
}
